package co.gov.medellin.ssti.sub.web.action;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.beans.factory.annotation.Autowired;

import co.gov.medellin.ssti.sub.common.excepcion.SUBException;
import co.gov.medellin.ssti.sub.model.dao.exception.DAOException;
import co.gov.medellin.ssti.sub.model.service.RepDetBOTService;
import co.gov.medellin.ssti.sub.model.vo.CondicionConsultaVO;
import co.gov.medellin.ssti.sub.model.vo.RepAtributoVO;
import co.gov.medellin.ssti.sub.model.vo.RepDetBOTVO;
import co.gov.medellin.ssti.sub.model.vo.ReporteDetVO;

public class ReporteFinalAction extends Action {
	@Autowired
	RepDetBOTService repDetBOTServiceImpl2;

	/**
	 * This is the main action called from the Struts framework.
	 * 
	 * @param mapping
	 *            The ActionMapping used to select this instance.
	 * @param form
	 *            The optional ActionForm bean for this request.
	 * @param request
	 *            The HTTP Request we are processing.
	 * @param response
	 *            The HTTP Response we are processing.
	 * @return
	 * @throws java.io.IOException
	 * @throws javax.servlet.ServletException
	 */
	@SuppressWarnings("unchecked")
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {

		HttpSession session = request.getSession();

		String subpro = (String) session.getAttribute("generar");
		String sede = (String) session.getAttribute("sede");
		List<RepAtributoVO> lGeneral = new LinkedList<RepAtributoVO>();
		List<RepAtributoVO> lEspecifico = new LinkedList<RepAtributoVO>();

		lGeneral = (List<RepAtributoVO>) session.getAttribute("listageneral");
		lEspecifico = (List<RepAtributoVO>) session
				.getAttribute("listaEspecificos");
		List<CondicionConsultaVO> lFiltro = (List<CondicionConsultaVO>) session
				.getAttribute("ListaFiltrado");
		String fechaInicial = (String) session.getAttribute("fechaInicial");
		String fechaFinal = (String) session.getAttribute("fechaFinal");
		String codigoAtrGestor = (String) session
				.getAttribute("codigoAtrGestor");
		String codigovfl = (String) session.getAttribute("codigovfl");

		if (lGeneral.size() != 0 || lEspecifico.size() != 0) {

			int itamanoEsp = lEspecifico.size();
			String tamanoEsp = String.valueOf(itamanoEsp);
			int itamanoGen = lGeneral.size();
			String tamanoGen = String.valueOf(itamanoGen);

			session.setAttribute("TamanoEsp", tamanoEsp);
			session.setAttribute("TamanoGen", tamanoGen);

			RepDetBOTVO criterea = new RepDetBOTVO();
			criterea.setCodeSede(new Long(sede));
			criterea.setCodeSubprograma(new Long(subpro));

			if (fechaInicial != null && fechaFinal != null
					&& !fechaInicial.equalsIgnoreCase("")
					&& !fechaFinal.equalsIgnoreCase("")) {
				criterea.setDesde(fechaInicial);
				criterea.setHasta(fechaFinal);
			}
			if (codigoAtrGestor != null && codigovfl != null) {
				criterea.setCodigoAtrGestor(codigoAtrGestor);
				criterea.setGestor(codigovfl);
			}

			try {
				// cantidadBenOtor = BOTDAO.getCantidadBeneOtorg(criterea);
				criterea.setCantidadBeneficiarios(repDetBOTServiceImpl2
						.findCantidadBeneficiarios(lFiltro, criterea));
				criterea.setValor(repDetBOTServiceImpl2.findValorTotal(lFiltro,
						criterea));
				session.setAttribute("Parametros", criterea);
				List<ReporteDetVO> reporte = (List<ReporteDetVO>) repDetBOTServiceImpl2
						.findReporte(lGeneral, lEspecifico, lFiltro, criterea);
				criterea.setEntregas(new Integer(reporte.size()));
				// TODO ???
				repDetBOTServiceImpl2.findFilters(criterea);

				session.setAttribute("repDetBeneficio", reporte);
			} catch (DAOException e) {
				SUBException.logDebugError("", e);
			}

		} else {
			request.setAttribute(
					"msg",
					"Seleccione alguna opcion en las listas de atributos del beneficio o del beneficiario.");
		}

		return mapping.findForward("success");
	}
}